package sj;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.traversal.NodeIterator;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

public class demo1 {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
        //得到DOM解析器工厂实例
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newDefaultInstance();
        //从DOM工厂获得DOM解析器
        DocumentBuilder db=dbf.newDocumentBuilder();
        //解析XML文档，得到一个Document对象，及DOM树
        Document doc=db.parse("src/sj/demo1.xml");
        NodeList list=doc.getElementsByTagName("Brand");
        //循环Brand信息
        for (int i=0;i<list.getLength();i++){
            //获取第i个Brand
            Node brand=list.item(i);

            Element element= (Element) brand;
            String attrValue=element.getAttribute("name");
            //获取子元素name属性值
            NodeList list1=element.getChildNodes();
            for (int j=0;j<list1.getLength();j++){
                Element typeElement= ((Element) list1.item(j));
                String type=typeElement.getAttribute("name");
                System.out.println("手机："+attrValue+type);
            }
        }
    }
}
